package shared;

import java.util.HashSet;

/**
 * WordElement holds a word and a set of urls
 * 
 */
public class WordElement 
{	
	
	//Instance variables
	public String word;
	public HashSet<String> urls;
	
	/**
	 * WordElement constructor 
	 * 
	 * @param w the word
	 */
	public WordElement(String w)
	{
		//Set word
		word = w;
		
		//Create new HashSet
		urls = new HashSet<String>();
	}
	
	/**
	 * addUrl adds a url the the WordElements urls set
	 * 
	 * @param url string
	 */
	public void addUrl(String url)
	{
		urls.add(url);
	}
	
	@Override
	/**
	 * toString prints word and urls of the WordElement
	 */
	public String toString()
	{
		//Add word
		String returnValue = word.toString() + "\n";
		
		//Add the urls
		for(String url: urls)
		{
			returnValue += url.toString() + "\n";
		}
		
		//Return value
		return returnValue;
	}
	
	@Override	
	/**
	 * If the other object is a WordElement, has the same word as 
	 * this WordElement and it's urls is equal to this WordElements 
	 * urls then they are equal
	 * 
	 */
	public boolean equals(Object o)
	{
		//Create return value
		boolean returnValue = false;
		
		//Check if the other object is a WordElement and not null
		if(o instanceof WordElement && o != null)
		{
			//Create variable for other WordElement
			WordElement other = (WordElement) o;
			
			//Check if the word and urls are equal
			if(this.word.equals(other.word) && this.urls.equals(other.urls))
			{
				//Set value to true
				returnValue = true;
			}			
		}
		
		//Return value
		return returnValue;
	}

}
